layui.define(['zlContext'], function (exports) {
    'use strict';
    var $ = layui.$
        , form = layui.form
        , zlContext = layui.zlContext
        , formindex
        , zlshenhjm = {
            render: function (processid,url,params,formres,callback) {
                zlContext.post(layui.cache['contentPath'] + "/core/workflow/model/getformdata", processid, function (data) {
                    data=data.obj;
                    var formparams={};
                    var titleparams={};
                    if(data!=null){
                        $.each(data ,function(index,record){
                            formparams[record.id]=record.value;                        
                        });
                    }
                    formres = $.extend({}, formparams,formres) ;
                    zlshenhjm.openform(processid,url,params,formres,callback,data);
                });
            },
            openform: function (processid,url,params,formres,callback,data) {
                var html = ['<form class="layui-form " style="margin:8px" id="addnew" >'
                    , '   <div class="layui-form-item">'
                    , '       <div class="layui-col-md12 ">'
                    , '           <label class="layui-form-label">流程操作：</label>'
                    , '           <div class="layui-input-block">'
                    , '				   <input type="radio" id="canSongs" name="confirm" value="1" lay-filter="check" title="送审/提交">'
                    , '				   <input type="radio" id="canTongy" name="confirm" value="3" lay-filter="check" title="同意">'
                    , '				   <input type="radio" id="canTuih" name="confirm" value="0" lay-filter="check" title="退回">'
                    , '				   <input type="radio" id="canButy" name="confirm" value="2" lay-filter="check" title="不同意">'
                    , '				   <input type="radio" id="canQuh" name="confirm" value="4" lay-filter="check" title="取回">'
                    , '				   <input type="hidden" id="inputcanSongs" >'
                    , '				   <input type="hidden" id="inputcanTongy" >'
                    , '				   <input type="hidden" id="inputcanTuih" >'
                    , '				   <input type="hidden" id="inputcanButy" >'
                    , '				   <input type="hidden" id="inputcanQuh" >'               
                    , '           </div>'
                    , '       </div>'
                    , '   </div>'
                    , '   <div class="layui-form-item">'
                    , '       <div class="layui-col-md12">'
                    , '           <label class="layui-form-label">审批意见：</label>'
                    , '           <div class="layui-input-block">'
                    , '               <textarea class="layui-textarea" lay-filter="comment" maxlength="255" name="comment" style="width:90%"></textarea>'
                    , '           </div>'
                    , '       </div>'
                    , '   </div>'
                    , '   <div class="layui-form-btn-box">'
                    , '       <button class="layui-btn" id="btnadd" lay-submit  lay-filter="updateprocess" style="">提交</button>'
                    , '       <button class="layui-btn layui-btn-primary" lay-submit  lay-filter="cancel">取消</button>'
                    , '   </div>'
                    , '</form>'
                ];
                formindex = layer.open(
                    {
                        type: 1,
                        id: 'zlshenhjmCheckLayer',
                        content: html.join(""),
                        title: "流程审核",
                        area:'600px',
                        success: function (index) { 
                            form.render();
                            zlshenhjm.setcommit(processid, callback,url,params);
                            zlshenhjm.setvisibleBtn(formres,data);
                        }
                    }
                );
            },
            setvisibleBtn:function(formres,data){
                if (data && data.length > 0) {
                    $.each(data ,function(index,record){
                        $('#'+record.id).attr('title',record.name);
                        $('#input'+record.id).val(record.comment);
                    });
                }
                
                form.render();
                if(formres.canTuih=='false'){
                    $('#canTuih').next().css('display','none');
                }
                if(formres.canSongs=='false'){
                    $('#canSongs').next().css('display','none');
                }
                if(formres.canButy=='false'){
                    $('#canButy').next().css('display','none');
                }
                if(formres.canTongy=='false'){
                    $('#canTongy').next().css('display','none');
                }
                if(formres.canQuh=='false'){
                    $('#canQuh').next().css('display','none');
                }
            },
            setcommit: function (processid, callback,url,params) {
                form.on('submit(updateprocess)', function (data) {
                    if(data.field.confirm==undefined){
                        zlContext.errorMsg('请选择相应的流程操作！');
                        return false;
                    }
                    var param ={comment:data.field.comment,confirm: data.field.confirm,processid:processid};  
                    if(url){
                        params = $.extend({},params,param);     
                    }else {
                        url= layui.cache['contentPath'] + "/core/workflow/updateprocess";  
                    }               
                    zlContext.post(url, params, function (data) {
                        if (data.state == 'OK') {
                            layer.close(formindex);
                            if(callback!=undefined){
                                callback();
                            }else{
                                zlContext.successMsg('流程审核成功!');
                            }
                        }else{
                            zlContext.errorMsg(data.msg);
                        }
                    });
                    return false;
                });

                form.on('submit(cancel)', function (data){
                    layer.close(formindex);
                    return false;
                })

                form.on('radio(check)', function (data) {
                    $('textarea[name="comment"]').val($('#input'+data.elem.id).val());
                });
            }
        };
    exports('zlshenhjm', zlshenhjm);
});